package com.magina.antiPro.util;


import com.magina.antiPro.model.AccessToken;
import com.magina.antiPro.model.Members;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import java.io.InputStream;
import java.util.Formatter;

/**
 * 微信企业号调用类 {"errcode":0,"errmsg":"ok"} 此结果表示调用方法成功返回
 * Created by Cerulean on 2018/8/25.
 */
@Slf4j
public class QiYeWeiXinUtil {

    //获取access_token
    public static AccessToken access_token() {
        AccessToken accessToken = null;
        String id = "wxaf0fa46bc0a992f2";
        String corpsecret = "2e0c2f9fa329df40920bbb8843cc139a";
//        String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=Id&corpsecret=Secrect";
        String urlNameString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=Id&secret=corpsecret";
        urlNameString = urlNameString.replace("Id", id);
        urlNameString = urlNameString.replace("corpsecret", corpsecret);
        String result = "";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(urlNameString);//这里发送get请求
            // 获取当前客户端对象
            @SuppressWarnings({"resource", "deprecation"})
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    result = IOUtils.toString(instream, "utf-8");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        accessToken = (AccessToken) JsonMapper.fromJsonString(result, AccessToken.class);
        if (accessToken != null) {
            return accessToken;
        }
        return null;
    }

    //获取UserId
    public static String getUserId(String accessToken, String code) {
        String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE";
        urlNameString = urlNameString.replace("ACCESS_TOKEN", accessToken);
        urlNameString = urlNameString.replace("CODE", code);
        String result = "";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(urlNameString);//这里发送get请求
            // 获取当前客户端对象
            @SuppressWarnings({"resource", "deprecation"})
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    result = IOUtils.toString(instream, "utf-8");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JSONObject jsonObject = JSONObject.fromObject(result);
        return jsonObject.getString("UserId");
    }

    //获取name,email,mobile
    public static Members getMembers(String accessToken, String userid)  {
        Members members = new Members();
        String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
        urlNameString = urlNameString.replace("ACCESS_TOKEN", accessToken);
        urlNameString = urlNameString.replace("USERID", userid);
        String result = "";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(urlNameString);//这里发送get请求
            // 获取当前客户端对象
            @SuppressWarnings({"resource", "deprecation"})
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    result = IOUtils.toString(instream, "utf-8");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.fromObject(result);
        members.setName(jsonObject.getString("name"));
        members.setEmail(jsonObject.getString("email"));
        members.setMobile(jsonObject.getString("mobile"));
        return members;
    }

    //获取jsapi_ticket
    public static String jsapi_ticket(String accessToken) {
        String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN";
        urlNameString = urlNameString.replace("ACCESS_TOKEN", accessToken);
        String result = "";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(urlNameString);//这里发送get请求
            // 获取当前客户端对象
            @SuppressWarnings({"resource", "deprecation"})
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    result = IOUtils.toString(instream, "utf-8");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JSONObject jsonObject = JSONObject.fromObject(result);
        return jsonObject.getString("ticket");
    }

    public static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}

